package com.dmxh.service.impl;

import com.dmxh.mapper.TbBrandMapper;
import com.dmxh.pojo.TbBrand;
import com.dmxh.pojo.TbBrandExample;
import com.dmxh.params.QueryParams;
import com.dmxh.result.PageList;
import com.dmxh.service.BrandService;
import com.dmxh.vo.BrandVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private TbBrandMapper tbBrandMapper;

    @Override
    public PageList<BrandVo> queryPage(QueryParams<BrandVo> queryParams) {
        PageHelper.startPage(queryParams.getPageNum(), queryParams.getPageSize());
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria criteria = tbBrandExample.createCriteria();
        if (!"".equals(queryParams.getTrimParam().getName())) {
            criteria.andNameLike(queryParams.getTrimParam().getName());
        }
        if (!"".equals(queryParams.getTrimParam().getFirstChar())) {
            criteria.andFirstCharLike(queryParams.getTrimParam().getFirstChar());
        }
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(tbBrandExample);
        List<BrandVo> brandVos = tbBrands.stream().map(tbBrand -> {
            BrandVo brandVo = new BrandVo();
            brandVo.setId(tbBrand.getId());
            brandVo.setFirstChar(tbBrand.getFirstChar());
            brandVo.setName(tbBrand.getName());
            return brandVo;
        }).collect(Collectors.toList());
        PageInfo<TbBrand> info = new PageInfo<>(tbBrands);
        return new PageList<>(info.getPageNum(),info.getPageSize(),info.getTotal(),brandVos);
    }

    @Override
    public BrandVo queryBrandByName(String name) {
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria c = tbBrandExample.createCriteria();
        c.andNameEqualTo(name);
        List<TbBrand> tbBrands = tbBrandMapper.selectByExample(tbBrandExample);
        List<BrandVo> collect = tbBrands.stream().map(BrandVo -> new BrandVo(BrandVo.getId(), BrandVo.getName(), BrandVo.getFirstChar())).collect(Collectors.toList());
        return collect.get(0);
    }

    @Override
    public void addInfo(TbBrand tbBrand) {
        tbBrandMapper.insertSelective(tbBrand);
    }

    @Override
    public void updateInfo(TbBrand tbBrand) {
        tbBrandMapper.updateByPrimaryKeySelective(tbBrand);
    }

    @Override
    public void deleteById(Integer id) {
        tbBrandMapper.deleteByPrimaryKey(id.longValue());
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        List<Long> collect = list.stream().map(Integer::longValue).collect(Collectors.toList());
        TbBrandExample tbBrandExample = new TbBrandExample();
        TbBrandExample.Criteria c = tbBrandExample.createCriteria();
        c.andIdIn(collect);
        tbBrandMapper.deleteByExample(tbBrandExample);
    }

    @Override
    public List<BrandVo> queryAllBrand() {
        List<BrandVo> collect = tbBrandMapper.selectByExample(null).stream().map(tbBrand -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(tbBrand, brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return collect;
    }

}
